#include "StdAfx.h"

Chatbox* Singleton<Chatbox>::m_instance = 0;

Chatbox::Chatbox() : m_text("Fonts\\FrizQT__.ttf", "Friz Quadrata TT", 6, 12)
{
	m_position.x = 10.0f;
	m_position.y = 555.0f;
	m_size.x = 330.0f;
	m_size.y = 100.0f;
	m_relScroll = 0;
	m_upClick = false;
	m_downClick = false;

	new CommandHandler;

	m_borderVertices[0].z = m_borderVertices[1].z = m_borderVertices[2].z = m_borderVertices[3].z = 0.0f;
	m_borderVertices[0].rhw = m_borderVertices[1].rhw = m_borderVertices[2].rhw = m_borderVertices[3].rhw = 1.0f;
	m_borderVertices[0].x = m_position.x + 30;
	m_borderVertices[0].y = m_position.y;
	m_borderVertices[0].u = 1.0f;
	m_borderVertices[0].v = 0.0f;

	m_borderVertices[1].x = m_position.x + m_size.x;
	m_borderVertices[1].y = m_position.y;
	m_borderVertices[1].u = 1.0f;
	m_borderVertices[1].v = 112.0f / 127.0f;

	m_borderVertices[2].x = m_position.x + m_size.x;
	m_borderVertices[2].y = m_position.y + m_size.y;
	m_borderVertices[2].u = 0.0f;
	m_borderVertices[2].v = 112.0f / 127.0f;

	m_borderVertices[3].x = m_position.x + 30;
	m_borderVertices[3].y = m_position.y + m_size.y;
	m_borderVertices[3].u = 0.0f;
	m_borderVertices[3].v = 0.0f;

	m_baseVertices[0].z = m_baseVertices[1].z = m_baseVertices[2].z = m_baseVertices[3].z = 0.0f;
	m_baseVertices[0].rhw = m_baseVertices[1].rhw = m_baseVertices[2].rhw = m_baseVertices[3].rhw = 1.0f;
	m_baseVertices[0].x = m_position.x + 8 + 30;
	m_baseVertices[0].y = m_position.y + 5;
	m_baseVertices[0].u = 0.0f;
	m_baseVertices[0].v = 0.0f;

	m_baseVertices[1].x = m_position.x + m_size.x - 17;
	m_baseVertices[1].y = m_position.y + 5;
	m_baseVertices[1].u = 1.0f;
	m_baseVertices[1].v = 0.0f;

	m_baseVertices[2].x = m_position.x + m_size.x - 17;
	m_baseVertices[2].y = m_position.y + m_size.y - 4;
	m_baseVertices[2].u = 1.0f;
	m_baseVertices[2].v = 1.0f;

	m_baseVertices[3].x = m_position.x + 8 + 30;
	m_baseVertices[3].y = m_position.y + m_size.y - 4;
	m_baseVertices[3].u = 0.0f;
	m_baseVertices[3].v = 1.0f;

	m_upVertices[0].z = m_upVertices[1].z = m_upVertices[2].z = m_upVertices[3].z = 0.0f;
	m_upVertices[0].rhw = m_upVertices[1].rhw = m_upVertices[2].rhw = m_upVertices[3].rhw = 1.0f;
	m_upVertices[0].x = m_position.x;
	m_upVertices[0].y = m_position.y + 5;
	m_upVertices[0].u = 0.0f;
	m_upVertices[0].v = 0.0f;

	m_upVertices[1].x = m_position.x + 28;
	m_upVertices[1].y = m_position.y + 5;
	m_upVertices[1].u = 28.0f / 32.0f;
	m_upVertices[1].v = 0.0f;

	m_upVertices[2].x = m_position.x + 28;
	m_upVertices[2].y = m_position.y + 33;
	m_upVertices[2].u = 28.0f / 32.0f;
	m_upVertices[2].v = 28.0f / 32.0f;

	m_upVertices[3].x = m_position.x;
	m_upVertices[3].y = m_position.y + 33;
	m_upVertices[3].u = 0.0f;
	m_upVertices[3].v = 28.0f / 32.0f;

	m_downVertices[0].z = m_downVertices[1].z = m_downVertices[2].z = m_downVertices[3].z = 0.0f;
	m_downVertices[0].rhw = m_downVertices[1].rhw = m_downVertices[2].rhw = m_downVertices[3].rhw = 1.0f;
	m_downVertices[0].x = m_position.x;
	m_downVertices[0].y = m_position.y + m_size.y - 33;
	m_downVertices[0].u = 0.0f;
	m_downVertices[0].v = 0.0f;

	m_downVertices[1].x = m_position.x + 28;
	m_downVertices[1].y = m_position.y + m_size.y - 33;
	m_downVertices[1].u = 28.0f / 32.0f;
	m_downVertices[1].v = 0.0f;

	m_downVertices[2].x = m_position.x + 28;
	m_downVertices[2].y = m_position.y + m_size.y - 5;
	m_downVertices[2].u = 28.0f / 32.0f;
	m_downVertices[2].v = 28.0f / 32.0f;

	m_downVertices[3].x = m_position.x;
	m_downVertices[3].y = m_position.y + m_size.y - 5;
	m_downVertices[3].u = 0.0f;
	m_downVertices[3].v = 28.0f / 32.0f;

	m_borderFrame = CTextureHandler::GetInstance()->GetTexture("Interface\\CHATFRAME\\ChatFrameBorder.blp");
	m_baseFrame = CTextureHandler::GetInstance()->GetTexture("Interface\\Tooltips\\ChatBubble-Background.blp");
	m_upScroll = CTextureHandler::GetInstance()->GetTexture("Interface\\CHATFRAME\\UI-ChatIcon-ScrollUp-Up.blp");
	m_downScroll = CTextureHandler::GetInstance()->GetTexture("Interface\\CHATFRAME\\UI-ChatIcon-ScrollDown-Up.blp");
	m_downScrollDis = CTextureHandler::GetInstance()->GetTexture("Interface\\CHATFRAME\\UI-ChatIcon-ScrollDown-Disabled.blp");
	m_upScrollDis = CTextureHandler::GetInstance()->GetTexture("Interface\\CHATFRAME\\UI-ChatIcon-ScrollUp-Disabled.blp");
	m_upScrollDown = CTextureHandler::GetInstance()->GetTexture("Interface\\CHATFRAME\\UI-ChatIcon-ScrollUp-Down.blp");
	m_downScrollDown = CTextureHandler::GetInstance()->GetTexture("Interface\\CHATFRAME\\UI-ChatIcon-ScrollDown-Down.blp");
	CTextureHandler::GetInstance()->GetTexture("Interface\\ChatFrame\\UI-ChatInputBorder-Left.blp");
	CTextureHandler::GetInstance()->GetTexture("Interface\\Tooltips\\ChatBubble-Backdrop.blp");

}

void Chatbox::Resize()
{
	m_position.y = sD3D.GetSize().y - 213.0f;
	m_borderVertices[0].z = m_borderVertices[1].z = m_borderVertices[2].z = m_borderVertices[3].z = 0.0f;
	m_borderVertices[0].rhw = m_borderVertices[1].rhw = m_borderVertices[2].rhw = m_borderVertices[3].rhw = 1.0f;
	m_borderVertices[0].x = m_position.x + 30;
	m_borderVertices[0].y = m_position.y;
	m_borderVertices[0].u = 1.0f;
	m_borderVertices[0].v = 0.0f;

	m_borderVertices[1].x = m_position.x + m_size.x;
	m_borderVertices[1].y = m_position.y;
	m_borderVertices[1].u = 1.0f;
	m_borderVertices[1].v = 112.0f / 127.0f;

	m_borderVertices[2].x = m_position.x + m_size.x;
	m_borderVertices[2].y = m_position.y + m_size.y;
	m_borderVertices[2].u = 0.0f;
	m_borderVertices[2].v = 112.0f / 127.0f;

	m_borderVertices[3].x = m_position.x + 30;
	m_borderVertices[3].y = m_position.y + m_size.y;
	m_borderVertices[3].u = 0.0f;
	m_borderVertices[3].v = 0.0f;

	m_baseVertices[0].z = m_baseVertices[1].z = m_baseVertices[2].z = m_baseVertices[3].z = 0.0f;
	m_baseVertices[0].rhw = m_baseVertices[1].rhw = m_baseVertices[2].rhw = m_baseVertices[3].rhw = 1.0f;
	m_baseVertices[0].x = m_position.x + 8 + 30;
	m_baseVertices[0].y = m_position.y + 5;
	m_baseVertices[0].u = 0.0f;
	m_baseVertices[0].v = 0.0f;

	m_baseVertices[1].x = m_position.x + m_size.x - 17;
	m_baseVertices[1].y = m_position.y + 5;
	m_baseVertices[1].u = 1.0f;
	m_baseVertices[1].v = 0.0f;

	m_baseVertices[2].x = m_position.x + m_size.x - 17;
	m_baseVertices[2].y = m_position.y + m_size.y - 4;
	m_baseVertices[2].u = 1.0f;
	m_baseVertices[2].v = 1.0f;

	m_baseVertices[3].x = m_position.x + 8 + 30;
	m_baseVertices[3].y = m_position.y + m_size.y - 4;
	m_baseVertices[3].u = 0.0f;
	m_baseVertices[3].v = 1.0f;

	m_upVertices[0].z = m_upVertices[1].z = m_upVertices[2].z = m_upVertices[3].z = 0.0f;
	m_upVertices[0].rhw = m_upVertices[1].rhw = m_upVertices[2].rhw = m_upVertices[3].rhw = 1.0f;
	m_upVertices[0].x = m_position.x;
	m_upVertices[0].y = m_position.y + 5;
	m_upVertices[0].u = 0.0f;
	m_upVertices[0].v = 0.0f;

	m_upVertices[1].x = m_position.x + 28;
	m_upVertices[1].y = m_position.y + 5;
	m_upVertices[1].u = 28.0f / 32.0f;
	m_upVertices[1].v = 0.0f;

	m_upVertices[2].x = m_position.x + 28;
	m_upVertices[2].y = m_position.y + 33;
	m_upVertices[2].u = 28.0f / 32.0f;
	m_upVertices[2].v = 28.0f / 32.0f;

	m_upVertices[3].x = m_position.x;
	m_upVertices[3].y = m_position.y + 33;
	m_upVertices[3].u = 0.0f;
	m_upVertices[3].v = 28.0f / 32.0f;

	m_downVertices[0].z = m_downVertices[1].z = m_downVertices[2].z = m_downVertices[3].z = 0.0f;
	m_downVertices[0].rhw = m_downVertices[1].rhw = m_downVertices[2].rhw = m_downVertices[3].rhw = 1.0f;
	m_downVertices[0].x = m_position.x;
	m_downVertices[0].y = m_position.y + m_size.y - 33;
	m_downVertices[0].u = 0.0f;
	m_downVertices[0].v = 0.0f;

	m_downVertices[1].x = m_position.x + 28;
	m_downVertices[1].y = m_position.y + m_size.y - 33;
	m_downVertices[1].u = 28.0f / 32.0f;
	m_downVertices[1].v = 0.0f;

	m_downVertices[2].x = m_position.x + 28;
	m_downVertices[2].y = m_position.y + m_size.y - 5;
	m_downVertices[2].u = 28.0f / 32.0f;
	m_downVertices[2].v = 28.0f / 32.0f;

	m_downVertices[3].x = m_position.x;
	m_downVertices[3].y = m_position.y + m_size.y - 5;
	m_downVertices[3].u = 0.0f;
	m_downVertices[3].v = 28.0f / 32.0f;
}

void Chatbox::Draw()
{
	if(!m_visible || sD3D.GetAppState() != STATE_WORLD)
		return;
	sD3D->SetFVF(VERTEX3FRHWT);
	sD3D->SetTexture(0, *m_borderFrame);
	sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, m_borderVertices, sizeof(Vertex3FRHWT));
	sD3D->SetTexture(0, *m_baseFrame);
	sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, m_baseVertices, sizeof(Vertex3FRHWT));
	if(m_relScroll + 7 < m_lines.size())
	{
		if(!m_upClick)
			sD3D->SetTexture(0, *m_upScroll);
		else
			sD3D->SetTexture(0, *m_upScrollDown);
	}
	else
		sD3D->SetTexture(0, *m_upScrollDis);
	sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, m_upVertices, sizeof(Vertex3FRHWT));
	if(m_relScroll != 0)
	{
		if(!m_downClick)
			sD3D->SetTexture(0, *m_downScroll);
		else
			sD3D->SetTexture(0, *m_downScrollDown);
	}
	else
		sD3D->SetTexture(0, *m_downScrollDis);
	sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, m_downVertices, sizeof(Vertex3FRHWT));
	if(m_lines.size() <= 7)
	{
		for(ui32 i = 0; i < m_lines.size(); ++i)
		{
			m_text.DrawTextA(ui32(m_position.x + 10 + 30), ui32(m_position.y + m_size.y - 19 - (i * 12)), m_text.GetTextLen(m_lines[m_lines.size() - 1 - i].szText), 28, m_lines[m_lines.size() - 1 - i].szText, m_lines[m_lines.size() - 1 - i].dwColor);
		}
	}
	else
	{
		for(ui32 i = 0; i < 7; ++i)
		{
			m_text.DrawTextA(ui32(m_position.x + 10 + 30), ui32(m_position.y + m_size.y - 19 - (i * 12)), m_text.GetTextLen(m_lines[m_lines.size() - 1 - i - m_relScroll].szText), 28, m_lines[m_lines.size() - 1 - i - m_relScroll].szText, m_lines[m_lines.size() - 1 - i].dwColor);
		}
	}
}

void Chatbox::AddText(std::string szText, i32 texEntry, ui32 dwColor)
{
	while(m_lines.size() >= 60)
		m_lines.erase(m_lines.begin());
	ChatboxText t;
	t.szText = sLocale.GetLocale(texEntry, szText);
	t.dwColor = dwColor;
	m_lines.push_back(t);
	m_relScroll = 0;
}

void Chatbox::MouseActivity(bool left, Vertex2F pos, bool up)
{
	if(pos.x <= m_position.x + 28 && pos.y >= m_position.y + 5 && pos.y <= m_position.y + 33)
	{
		m_downClick = false;
		if(!up)
			m_upClick = true;
		else
		{
			if(m_relScroll + 7 < m_lines.size())
				m_relScroll += 1;
			m_upClick = false;
		}
	}
	else if(pos.x <= m_position.x + 28 && pos.y >= m_position.y + m_size.y - 33 && pos.y <= m_position.y + m_size.y - 5)
	{
		m_upClick = false;
		if(!up)
			m_downClick = true;
		else
		{
			if(m_relScroll != 0)
				m_relScroll -= 1;
			m_downClick = false;
		}
	}
	else
	{
		m_upClick = m_downClick = false;
	}
}